package com.gerzel.stop.server;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class test extends HttpServlet {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        
        @SuppressWarnings("unchecked")
		public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
                if (user != null) {
                	PersistenceManager pm = PMF.get().getPersistenceManager();
                    resp.setContentType("text/plain; charset=UTF-8"); 
                    Query query = pm.newQuery(UserHabits.class, "UserName == UserNameParam");
                    query.declareParameters("com.google.appengine.api.users.User UserNameParam");
                    try {
        		        List<UserHabits> result = (List<UserHabits>) query.execute(user);
        		        if (result!=null && !result.isEmpty()) {
        		        	resp.getWriter().println("Что-то есть!");
                                resp.getWriter().println(result.toString());
                                resp.getWriter().println(result.get(0).getUserName().toString());
                                resp.getWriter().println(result.get(0).getDrinkDate().toString());
                                resp.getWriter().println(result.get(0).getSmokeDate().toString());
                                /*for (UserHabits r : result)
                                {
                                        resp.getWriter().println(r.getUserName().toString());
                                        resp.getWriter().println(r.getDrinkDate().toString());
                                        resp.getWriter().println(r.getSmokeDate().toString());
                                }*/
                        } else {
                                resp.getWriter().println("Почему-то пусто...");
                                Date drinkDate = new Date();
                                Date smokeDate = new Date();
                                UserHabits habits = new UserHabits(user, drinkDate, smokeDate);
            	            	try {
            	            		pm.makePersistent(habits);
            	            		} finally {
            	            			pm.close();
            	            			};
                        }
                    } finally {
                        query.closeAll();
                    }
                    } else {
                        resp.getWriter().println("Нет логина");
                        }
                }
}